/*
 * This file is part of the EasyLogger Library.
 *
 * Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Function: Portable interface for each platform.
 * Created on: 2015-04-28
 */
#include <elog.h>

#if USING_POSIX_API
    #include "pthread.h"
    #include "semaphore.h"
    #include <aos/cli.h>

    #define printf aos_cli_printf
    static sem_t output_lock;
#else
    #define printf rt_kprintf
    static struct rt_semaphore output_lock;
#endif

#ifdef ELOG_ASYNC_OUTPUT_ENABLE
    #if USING_POSIX_API
        static sem_t output_notice;
    #else
        static struct rt_semaphore output_notice;
    #endif
    static void *async_output(void *arg);
#endif

/**
 * EasyLogger port initialize
 *
 * @return result
 */
ElogErrCode elog_port_init(void)
{
    ElogErrCode result = ELOG_NO_ERR;

#if USING_POSIX_API
    sem_init(&output_lock, 0, 1);
#else
    /* add your code here */
    rt_sem_init(&output_lock, "elog lock", 1, RT_IPC_FLAG_PRIO);
#endif

#ifdef ELOG_ASYNC_OUTPUT_ENABLE
#if USING_POSIX_API
    void *status = NULL;
    pthread_t async_thread;

    sem_init(&output_notice, 0, 0);
    result = pthread_create(&async_thread, NULL, async_output, NULL);
#else
    rt_thread_t async_thread = NULL;

    rt_sem_init(&output_notice, "elog async", 0, RT_IPC_FLAG_PRIO);

    async_thread = rt_thread_create("elog_async", async_output, NULL, 4096, RT_THREAD_PRIORITY_MAX - 1, 10);
    if (async_thread)
    {
        rt_thread_startup(async_thread);
    }
#endif /*USING_POSIX_API*/
#endif /*ELOG_ASYNC_OUTPUT_ENABLE*/

    return result;
}

/**
 * EasyLogger port deinitialize
 *
 */
void elog_port_deinit(void)
{

    /* add your code here */
}

/**
 * output log port interface
 *
 * @param log output of log
 * @param size log size
 */
void elog_port_output(const char *log, size_t size)
{

    /* add your code here */
    /* output to terminal */
    printf("%.*s\r", size, log);
}

/**
 * output lock
 */
void elog_port_output_lock(void)
{
#if USING_POSIX_API
    sem_wait(&output_lock);
#else
    /* add your code here */
    rt_sem_take(&output_lock, RT_WAITING_FOREVER);
#endif
}

/**
 * output unlock
 */
void elog_port_output_unlock(void)
{
#if USING_POSIX_API
    sem_post(&output_lock);
#else
    /* add your code here */
    rt_sem_release(&output_lock);
#endif
}

/**
 * get current time interface
 *
 * @return current time
 */
const char *elog_port_get_time(void)
{
    /* add your code here */
    static char cur_system_time[16] = {0};
    uint32_t tick = 0u;
    struct timespec ts;

    clock_gettime(CLOCK_MONOTONIC, &ts);
    tick = ts.tv_sec * 100 * 1000 + ts.tv_nsec / (1000 * 1000);
    snprintf(cur_system_time, 16, "tick:%010d", (uint32_t)((tick + 100 - 1) / 100));
    return cur_system_time;
}

/**
 * get current process name interface
 *
 * @return current process name
 */
const char *elog_port_get_p_info(void)
{
    /* add your code here */
    return "";
}

/**
 * get current thread name interface
 *
 * @return current thread name
 */
const char *elog_port_get_t_info(void)
{
    /* add your code here */
#if USING_POSIX_API
#define THREAD_NAME_LEN 16
    static char thread_name[USING_POSIX_API];
    int result = pthread_getname_np(pthread_self(), thread_name, USING_POSIX_API);
    if (result != 0)
    {
        return "";
    }
    return thread_name;
#else
    return rt_thread_self()->parent.name;
#endif
}

#ifdef ELOG_ASYNC_OUTPUT_ENABLE
void elog_async_output_notice(void)
{
#if USING_POSIX_API
    sem_post(&output_notice);
#else
    rt_sem_release(&output_notice);
#endif
}

static void *async_output(void *arg)
{
    size_t get_log_size = 0;
    static char poll_get_buf[ELOG_LINE_BUF_SIZE - 4];

    while (true)
    {
#if USING_POSIX_API
        sem_wait(&output_notice);
#else
        /* waiting log */
        rt_sem_take(&output_notice, RT_WAITING_FOREVER);
#endif
        /* polling gets and outputs the log */
        while (true)
        {

#ifdef ELOG_ASYNC_LINE_OUTPUT
            get_log_size = elog_async_get_line_log(poll_get_buf, sizeof(poll_get_buf));
#else
            get_log_size = elog_async_get_log(poll_get_buf, sizeof(poll_get_buf));
#endif

            if (get_log_size)
            {
                elog_port_output(poll_get_buf, get_log_size);
            }
            else
            {
                break;
            }
        }
    }

    return 0;
}
#endif